import {
  AuthModel
} from '../../models/authData.js'
let authModel = new AuthModel()
import {
  PersonModel
} from '../../models/personal.js'
let personModel = new PersonModel()
const { $Toast } = require('../../components/base/index')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    trainExp: [],
    FItEmpId: '',
    visible: false,
    selectedIndex: 0,
  },

  openModal(e) {
    this.setData({
      visible: true,
      selectedIndex: e.currentTarget.id
    })
  },

  confirmDel(e) {
    this.setData({
      visible: false
    })
    this.deleteTrainExp(e)
  },

  cancelDel() {
    this.setData({
      visible: false
    })
  },

  deleteTrainExp(e) {
    var FId = this.data.trainExp[this.data.selectedIndex].FId
    $Toast({
      content: '加载中',
      type: 'loading'
    });
    personModel.deleteTrainExp({
      args: {
        entityIds: [FId]
      }
    }).then(res => {
      if (res.data.success) {
        $Toast({
          content: '删除成功',
          type: 'success'
        });
        this.requestData()
      } else {

      }
    })
  },

  addTrainExp() {
    wx.navigateTo({
      url: `/pages/editTrainExp/editTrainExp?FItEmpId=${this.data.FItEmpId}`,
    })
  },

  nextPage() {
    wx.reLaunch({
      url: '/pages/hhd-home/hhd-home',
    })
  },

  editTrainExp(e) {
    wx.navigateTo({
      url: `/pages/editTrainExp/editTrainExp?data=${JSON.stringify(this.data.trainExp[e.currentTarget.id])}&FItEmpId=${this.data.FItEmpId}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '完善培训经历'
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#3B80FE',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.requestData()
  },

  requestData() {
    $Toast({
      content: '加载中',
      type: 'loading'
    });
    authModel.getSelfAuthData()
      .then(res => {
        this.setData({
          trainExp: res.data.data.resumeData.trainingUndergoList,
          FItEmpId: res.data.data.resumeData.employee[0].FId
        })
        $Toast.hide()
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})